package vip.gadfly.tiktok.open.api.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.protocol.HttpService;
import vip.gadfly.tiktok.config.TtOpConfigStorage;
import vip.gadfly.tiktok.core.enums.TtOpTicketType;
import vip.gadfly.tiktok.core.util.HttpUtils;
import vip.gadfly.tiktok.open.api.TtOpCheckService;
import vip.gadfly.tiktok.open.bean.check.*;
import vip.gadfly.tiktok.open.common.ITtOpBaseService;
import vip.gadfly.tiktok.open.common.TtOpBaseResponse;

import javax.annotation.Resource;
import java.util.List;

import static vip.gadfly.tiktok.core.enums.TtOpApiUrl.OAuth2.*;

/**
 * wangyongjin
 */
@Slf4j
@RequiredArgsConstructor
public class TtOpCheckServiceImpl implements TtOpCheckService {

    private final ITtOpBaseService ttOpBaseService;

    protected TtOpConfigStorage getTtOpConfigStorage() {
        return this.ttOpBaseService.getTtOpConfigStorage();
    }



    @Override
    public TtOpPrepareResult prepare(String code,String encryptedData) {
        String rawUrl = CERTIFICATE_URL.getUrl(getTtOpConfigStorage());
        String accessToken = this.ttOpBaseService.getTicket(TtOpTicketType.CLIENT,false);
        Multimap<String, String> headers = LinkedListMultimap.create();
        headers.put("Content-Type", "application/json");
        headers.put("access-token",accessToken);
        String url = String.format(rawUrl, code, encryptedData);
        return this.ttOpBaseService.getWithHeader(url, headers,TtOpPrepareResult.class);
    }

    @Override
    public TtOpCheckResult verify(String verifyToken,String poiId, List<String> encryptedCode) {
        String rawUrl = CERTIFICATE_VERIFY_URL.getUrl(getTtOpConfigStorage());
        String accessToken = this.ttOpBaseService.getTicket(TtOpTicketType.CLIENT,false);
        TtOpVerifyRequest request = new TtOpVerifyRequest()
                .setVerifyToken(verifyToken)
                .setPoiId(poiId)
                .setEncryptedCodes(encryptedCode)
                ;
        Multimap<String, String> headers = LinkedListMultimap.create();
        //默认是application/json
        headers.put("access-token",accessToken);
        return this.ttOpBaseService.postWithHeaders(rawUrl, headers, request, TtOpCheckResult.class);
    }

    @Resource
    ObjectMapper mapper;

    @Override
    public TtOpStateResult verifyState(String encryptedCode) {
        String rawUrl = CERTIFICATE_VERIFY_STATE_URL.getUrl(getTtOpConfigStorage());
        String accessToken = this.ttOpBaseService.getTicket(TtOpTicketType.CLIENT,false);
        Multimap<String, String> headers = LinkedListMultimap.create();
//        headers.put("Content-Type", "application/json");
//        headers.put("access-token",accessToken);
        try {
            String url = String.format(rawUrl, encryptedCode,accessToken);
            String str = HttpUtils.executeHttpGet(url);
            JavaType type = mapper.getTypeFactory().constructParametricType(TtOpBaseResponse.class, TtOpStateResult.class);
            TtOpBaseResponse<TtOpStateResult> response = mapper.readValue(str, type);
            return response.getData();

        }catch (Exception ex){


        }
        return  null;

    }

    @Override
    public JSONObject verifyRecord(String cursor, Integer size,String accountId,Long startTime,Long endTime) {
        String rawUrl = CERTIFICATE_RECORD_URL.getUrl(getTtOpConfigStorage());
        String accessToken = this.ttOpBaseService.getTicket(TtOpTicketType.CLIENT,false);
        Multimap<String, String> headers = LinkedListMultimap.create();
        headers.put("Content-Type", "application/json");
        headers.put("access-token",accessToken);
        String url = String.format(rawUrl, size, cursor,accountId);
        if(startTime!=null){
            url+=String.format("&start_time=%s&end_time=%s", startTime,endTime);
        }
        return this.ttOpBaseService.getWithHeader(url, headers, JSONObject.class);
    }
}
